package com.bana.fvs.app.view;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bana.fvs.R;


/**
 * Author : yanftch
 * Date   : 2017/11/6
 * Time   : 15:34
 * Desc   : 登录或者注册中密码输入的组合控件
 */

public class PassWordInputLayout extends LinearLayout {

    private EditText mPassword;
    private ImageView mCha;
    private ImageView mPasswordSeeOrHint;

    private boolean mPasswordIsSee;  //标记密码是否可见

    public PassWordInputLayout(Context context) {
        super(context);
        init(context);
    }

    public PassWordInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PassWordInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {

        View view = View.inflate(context, R.layout.view_input_password_layout, null);

        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(view, lp);

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mPassword = (EditText) this.findViewById(R.id.etPassword);
        mCha = (ImageView) this.findViewById(R.id.ivClear);
        mPasswordSeeOrHint = (ImageView) this.findViewById(R.id.ivPasswordSeeOrHint);

        initListener();
    }

    private void initListener() {

        mPasswordSeeOrHint.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                changePasswordStyle();
            }
        });

        mCha.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mPassword.setText("");
            }
        });

        //监听密码输入文本的变化，动态控制清楚按钮的显示和隐藏
        mPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().length() > 0) {
                    if (mCha.getVisibility() == View.GONE) {
                        mCha.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mCha.getVisibility() == View.VISIBLE) {
                        mCha.setVisibility(GONE);
                    }
                }

                if (mOnEditTextChangeListener != null) {
                    mOnEditTextChangeListener.editTextChangeListener(editable);
                }
            }
        });
    }


    /**
     * 更改密码的样式
     */
    public void changePasswordStyle() {
        mPasswordIsSee = !mPasswordIsSee;
        if (mPasswordIsSee) {
            mPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            mPasswordSeeOrHint.setImageResource(R.drawable.password_see);
            mPassword.setSelection(mPassword.getText().toString().trim().length());
        } else {
            mPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            mPasswordSeeOrHint.setImageResource(R.drawable.password_hint);
            mPassword.setSelection(mPassword.getText().toString().trim().length());
        }
    }

    public void setmPassword(String password){
        mPassword.setText(password);
        mPassword.setSelection(password.length());
    }

    /**
     * 获取密码文本
     *
     * @return 密码文本
     */
    public String getPasswordText() {
        return mPassword.getText().toString().trim();
    }

    /**
     * 设置提示文字
     *
     * @param str 提示文字字样
     *            todo:可以使用自定义属性来实现，待日后研究
     */
    public void setHintText(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        mPassword.setHint(str);
    }


    /**
     * 设置文本变化的监听
     *
     * @param onEditTextChangeListener
     */
    public onEditTextChangeListener mOnEditTextChangeListener;

    public interface onEditTextChangeListener {
        void editTextChangeListener(Editable editable);
    }

    public void addOnEditTextChangeListener(onEditTextChangeListener onEditTextChangeListener) {
        mOnEditTextChangeListener = onEditTextChangeListener;
    }

    public void setPasswordSeeOrHint() {
        mPasswordSeeOrHint.setVisibility(View.GONE);
    }

    public void setPasswordInputType() {
        mPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
    }


    public EditText getmPassword() {
        return mPassword;
    }
}


